var tools = {
  baseURL: 'http://dev.cnskycc.club',
  // baseURL: 'http://money.cnskycc.com',

  setItem: function(key, value) {
    if(typeof value === "object") {
      value = JSON.stringify(value)
    }
    localStorage.setItem(key, value)
    return true
  },

  getItem: function(key) {
    var value = localStorage.getItem(key)
    if(!value) return false
    try {
      return JSON.parse(value)
    } catch (error) {
      return value
    }
  },

  removeItem: function(key) {
    return localStorage.removeItem(key)
  },

  setParams: function(options) {
    options.url = this.baseURL + options.url
    var token = tools.getItem('userInfo').token;
    console.log(token)
    var headers = !!token ? { authorization: "Bearer " + token } : {};
    return $.extend(true, {}, {
      type: 'GET',
      timeout: 10000,
      dataType: 'json',
      headers: headers,
      error: function(err) {
        if(err.status===403) {
        }
        /* layer.open({
          content: '网络连接出错', skin: 'msg', time: 2
        }); */
      }
    }, options)
  },

  ajax: function(options) {
    var params = this.setParams(options)
    $.ajax(params)
  },

  // 根据 key 获取 url 中的 value
  getQueryString: function(key) {
		var url = window.location.search;
		var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
		var result = url.substr(1).match(reg);
		return result ? decodeURIComponent(result[2]) : null;
  },

  filterTime: function(time, format) {
    format = format || 'yyyy-MM-dd hh:mm:ss';
    if(!time) return
    var date = new Date(parseInt(time)*1000);

    if(/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    var dt = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    }

    for(var key in dt) {
      if(new RegExp(`(${key})`).test(format)) {
        var str = dt[key] + ''
        format = format.replace(RegExp.$1,
          (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length)
        );
      }
    }
    return format;
  },

  filterNumber: function(num) {
    if(num >= 100000000) {
      return (num / 100000000).toFixed(2) + '亿'
    } else if(num >= 10000) {
      return (num / 10000).toFixed(2) + '万'
    } else {
      return num
    }
  }
}

var USER_ID = tools.getItem("userInfo").id;
if(USER_ID == 2 || USER_ID == 4) {
  var vConsole = new VConsole();
}
